/*****************************************************************************
#
#
******************************************************************************/
#include "EPD_2IN66B_Z90_Grey.h"
#include "DEBUG.h"
// #include "EPD_SDCard.h"

#define DEBUG Debug

static uint8_t iLut = 0;

/******************************************************************************
function :	Software reset
parameter:
******************************************************************************/
static void EPD_2IN66B_Z90_Grey_Reset(void)
{
    // DEV_Digital_Write(EPD_RST_PIN, 1);
    // DEV_Delay_ms(10);
    DEV_Digital_Write(EPD_RST_PIN, 0);
    DEV_Delay_ms(10);
    DEV_Digital_Write(EPD_RST_PIN, 1);
    DEV_Delay_ms(10);
}

/******************************************************************************
function :	send command
parameter:
     Reg : Command register
******************************************************************************/
static void EPD_2IN66B_Z90_Grey_SendCommand(UBYTE Reg)
{
    DEV_Digital_Write(EPD_DC_PIN, 0);
    // DEV_Digital_Write(EPD_CS_PIN, 0);
    DEV_SPI_WriteByte(Reg);
    // DEV_Digital_Write(EPD_CS_PIN, 1);
}

/******************************************************************************
function :	send data
parameter:
    Data : Write data
******************************************************************************/

static void EPD_2IN66B_Z90_Grey_SendData(UBYTE Data)
{
    DEV_Digital_Write(EPD_DC_PIN, 1);
    // DEV_Digital_Write(EPD_CS_PIN, 0);
    DEV_SPI_WriteByte(Data);
    // DEV_Digital_Write(EPD_CS_PIN, 1);
}

static void EPD_2IN66B_Z90_Grey_SendData2(UBYTE *Data, UDOUBLE length)
{
    DEV_SPI_WriteByte(Data, length);
}

static void EPD_2IN66B_Z90_Grey_SendData2(UBYTE Data)
{
    // DEV_Digital_Write(EPD_DC_PIN, 1);
    // DEV_Digital_Write(EPD_CS_PIN, 0);
    DEV_SPI_WriteByte(Data);
    // DEV_Digital_Write(EPD_CS_PIN, 1);
}


// https://www.mdpi.com/2072-666X/12/5/578
#define VSS  (0b00)
#define VH1  (0b01)
#define VSL  (0b10)
#define VH2  (0b11)
#define V(a,b,c,d)  (a<<6|b<<4|c<<2|d)

#define W___ V(VSL, VSS, VSS, VSS)
#define R___ V(VH2, VSS, VSS, VSS)
#define B___ V(VH1, VSS, VSS, VSS)
#define _B__ V(VSS, VH1, VSS, VSS)
#define _W__ V(VSS, VSL, VSS, VSS)
#define _R__ V(VSS, VH2, VSS, VSS)
#define BW__ V(VH1, VSL, VSS, VSS)
#define RW__ V(VH2, VSL, VSS, VSS)

#define __R_ V(VSS,VSS,VH2, VSS)
// #define W_R_ V(VSL, VSS, VH2, VSS)

// fast refresh for clock.
static const uint8_t lut_fast_bw[] = {
//  VS [0-11] phase [ABCD]
//     0     1     2     3     4     5     6     7     8     9    10    11
    B___, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT0 B
    _W__, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT1 W
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT2 R
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT3 NC?
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT4 VCOM
// 60: 
//  TP A, TP B, SRAB, TP C, TP D, SRCD,   RP
    0x04, 0x10, 0x03, 0x00, 0x00, 0x00, 0x02,   // 0
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 1
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 2
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 3
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 4
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 5
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 6
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 7
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 8
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 9
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 10
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 11
// 144: FR  25-200Hz
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
// 150: XON
    0x00, 0x00, 0x00,
// 153:
//  EOPT    VGH     VSH1    VSH2    VSL     VCOM
//  3F      03      04                      2C
//  22      -20v    15v     3v      -15v
    0x22,   0x17,   0x41,   0x94,   0x32,   0x36    
};

// fast full refresh.
static const uint8_t lut_full_bwr[] = {
//  VS [0-11] phase [ABCD]
//     0     1     2     3     4     5     6     7     8     9    10    11
    W___, BW__, B___, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT0 B
    _B__, BW__, _W__, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT1 W
    _R__, RW__, _W__, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT2 R
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT3 NC?
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT4 VCOM
// 60: 
//  TP A, TP B, SRAB, TP C, TP D, SRCD,   RP
    0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x02,   // 0
    0x09, 0x09, 0x01, 0x00, 0x00, 0x00, 0x02,   // 1
    0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x02,   // 2
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 3
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 4
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 5
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 6
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 7
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 8
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 9
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 10
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 11
// 144: FR  25-200Hz
    0x44, 0x40, 0x00, 0x00, 0x00, 0x00,
// 150: XON
    0x00, 0x00, 0x00,
// 153:
//  EOPT    VGH     VSH1    VSH2    VSL     VCOM
//  3F      03      04                      2C
//  22      -20v    15v     3v      -15v
    0x22,   0x17,   0x41,   0x94,   0x32,   0x36    
};


// LUT for BLE Gray display (8 steps)//8grey
static const uint8_t lut_grey8_bwr[] = {
//  VS [0-11] phase [ABCD]
//     0     1     2     3     4     5     6     7     8     9    10    11
    B___, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT0 B
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT1 W
    __R_, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT2 R
    __R_, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT3 NC?
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,     // LUT4 VCOM
// 60: 
//  TP A, TP B, SRAB, TP C, TP D, SRCD,   RP
    0x01, 0x00, 0x01, 0x3F, 0x00, 0x01, 0x01,   // 0
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 1
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 2
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 3
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 4
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 5
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 6
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 7
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 8
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 9
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 10
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // 11
// 144: FR  25-200Hz
    0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
// 150: XON
    0x00, 0x00, 0x00,
// 153:
//  EOPT    VGH     VSH1    VSH2    VSL     VCOM
//  3F      03      04                      2C
//  22      -20v    15v     3v      -15v
    0x22,   0x17,   0x41,   0x94,   0x32,   0x36    
};

static const uint8_t lut_grey8_bwr_array[7][7] = {
//  TP A, TP B, SRAB, TP C, TP D, SRCD,   RP  、、0    
    {0x02, 0x00, 0x01, 0x08, 0x00, 0x0C, 0x01}, // ilut1
    {0x01, 0x00, 0x01, 0x08, 0x00, 0x0C, 0x01},// ilut2
    {0x01, 0x00, 0x01, 0x08, 0x00, 0x0C, 0x01},// ilut3
    {0x01, 0x00, 0x01, 0x08, 0x00, 0x0C, 0x01}, // ilut4
    {0x01, 0x00, 0x01, 0x08, 0x00, 0x0C, 0x01}, // ilut5
    {0x01, 0x00, 0x01, 0x08, 0x00, 0x0E, 0x01}, // ilut6
    {0x01, 0x00, 0x01, 0x08, 0x00, 0x12, 0x01}, // ilut7
};

static void EPD_2IN66B_Z90_Grey_Lut(const unsigned char *lut)
{
    EPD_2IN66B_Z90_Grey_SendCommand(0x32);
    for(int i=0; i<153; i++) {
        EPD_2IN66B_Z90_Grey_SendData(lut[i]);
    }

    // gate voltage
    EPD_2IN66B_Z90_Grey_SendCommand(0x3F);
    EPD_2IN66B_Z90_Grey_SendData(*(lut+153));

    EPD_2IN66B_Z90_Grey_SendCommand(0x03);
    EPD_2IN66B_Z90_Grey_SendData(*(lut+154));

    // source voltage
    EPD_2IN66B_Z90_Grey_SendCommand(0x04);
    EPD_2IN66B_Z90_Grey_SendData(*(lut+155));    // VSH
    EPD_2IN66B_Z90_Grey_SendData(*(lut+156));    // VSH2
    EPD_2IN66B_Z90_Grey_SendData(*(lut+157));    // VSL

    EPD_2IN66B_Z90_Grey_SendCommand(0x2C);
    EPD_2IN66B_Z90_Grey_SendData(*(lut+158));
}


/******************************************************************************
function :	Wait until the busy_pin goes LOW
parameter:
******************************************************************************/
static void EPD_2IN66B_Z90_Grey_ReadBusy(void)
{
    DEBUG("e-Paper busy\r\n");
    while (1)
    {
        if (DEV_Digital_Read(EPD_BUSY_PIN) == 0)
            break;
        DEV_Delay_ms(10);
    }

    DEBUG("e-Paper busy release\r\n");
}



void EPD_2IN66B_Z90_Grey_ReadBusy(uint32_t ms)
{
    Debug("e-Paper busy:%d\r\n", millis());
    for (uint32_t i = 0; i < ms; i += 10)
   {
        if (!DEV_Digital_Read(EPD_BUSY_PIN))
        {
            Debug("e-Paper busy release:%d\r\n", millis());
            return;
        }
        if (ms > 200)
        {
#ifndef  TEST             
            EPD_BUSY_attachInterrupt(LOW);
#else
            DEV_Delay_ms(10);       
#endif
        }
        else
            DEV_Delay_ms(10);
    }
    Debug("e-Paper busy timeout:%d\r\n", millis());
}

/******************************************************************************
function :	Initialize the e-Paper register
parameter:
******************************************************************************/
void EPD_2IN66B_Z90_Grey_Init(uint8_t lut_type)
{

    EPD_2IN66B_Z90_Grey_Reset();

    EPD_2IN66B_Z90_Grey_ReadBusy(100);
    EPD_2IN66B_Z90_Grey_SendCommand(0x12); // SWRESET
    EPD_2IN66B_Z90_Grey_ReadBusy(100);

    EPD_2IN66B_Z90_Grey_SendCommand(0x01); // Driver output control
    EPD_2IN66B_Z90_Grey_SendData((EPD_2IN66B_Z90_Grey_HEIGHT - 1) % 256);
    EPD_2IN66B_Z90_Grey_SendData((EPD_2IN66B_Z90_Grey_HEIGHT - 1) / 256);
    EPD_2IN66B_Z90_Grey_SendData(0x00);

    EPD_2IN66B_Z90_Grey_SendCommand(0x11); // data entry mode
    EPD_2IN66B_Z90_Grey_SendData(0x03);

    EPD_2IN66B_Z90_Grey_SendCommand(0x44); // set Ram-X address start/end position
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    EPD_2IN66B_Z90_Grey_SendData(EPD_2IN66B_Z90_Grey_WIDTH / 8 - 1);

    EPD_2IN66B_Z90_Grey_SendCommand(0x45); // set Ram-Y address start/end position
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    EPD_2IN66B_Z90_Grey_SendData((EPD_2IN66B_Z90_Grey_HEIGHT - 1) % 256);
    EPD_2IN66B_Z90_Grey_SendData((EPD_2IN66B_Z90_Grey_HEIGHT - 1) / 256);

    EPD_2IN66B_Z90_Grey_SendCommand(0x3C); // BorderWavefrom
    EPD_2IN66B_Z90_Grey_SendData(0x05);

    EPD_2IN66B_Z90_Grey_SendCommand(0x21); //  Display update control
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    EPD_2IN66B_Z90_Grey_SendData(0x80);

    EPD_2IN66B_Z90_Grey_SendCommand(0x18); // Read built-in temperature sensor
    EPD_2IN66B_Z90_Grey_SendData(0x80);

    EPD_2IN66B_Z90_Grey_SendCommand(0x4E); // set RAM x address count to 0;
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    EPD_2IN66B_Z90_Grey_SendCommand(0x4F); // set RAM y address count to 0X199;
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    EPD_2IN66B_Z90_Grey_SendData(0x00);
    // EPD_2IN66B_Z90_Grey_SendData((EPD_2IN66B_Z90_Grey_HEIGHT - 1) % 256);
    // EPD_2IN66B_Z90_Grey_SendData((EPD_2IN66B_Z90_Grey_HEIGHT - 1) / 256);

        if (lut_type == EPD_2IN66B_Grey_Bwr8)   
        {
            uint8_t lut_temp[159];
            memcpy(lut_temp, lut_grey8_bwr, 159);
            if (iLut > 0 && iLut < 8)
            {
                lut_temp[60] = lut_grey8_bwr_array[iLut - 1][0];
                lut_temp[61] = lut_grey8_bwr_array[iLut - 1][1];
                lut_temp[62] = lut_grey8_bwr_array[iLut - 1][2];
                lut_temp[63] = lut_grey8_bwr_array[iLut - 1][3];
                lut_temp[64] = lut_grey8_bwr_array[iLut - 1][4];
                lut_temp[65] = lut_grey8_bwr_array[iLut - 1][5];
                lut_temp[66] = lut_grey8_bwr_array[iLut - 1][6];
            }
            EPD_2IN66B_Z90_Grey_Lut(lut_temp);          
          
        //   EPD_2IN66B_Z90_Grey_Lut(lut_grey8_bwr);
        }
    // EPD_2IN66B_Z90_Grey_Lut(lut_full_bwr);

    EPD_2IN66B_Z90_Grey_ReadBusy(100);
}

/******************************************************************************
function :	Clear screen
parameter:
******************************************************************************/
void EPD_2IN66B_Z90_Grey_Clear(void)
{
    UDOUBLE Width, Height;
    Width = (EPD_2IN66B_Z90_Grey_WIDTH % 8 == 0) ? (EPD_2IN66B_Z90_Grey_WIDTH / 8) : (EPD_2IN66B_Z90_Grey_WIDTH / 8 + 1);
    Height = EPD_2IN66B_Z90_Grey_HEIGHT;

    unsigned int i;
    EPD_2IN66B_Z90_Grey_SendCommand(0x24); // write RAM for black(0)/white (1)
    DEV_Digital_Write(EPD_DC_PIN, 1);
    for (i = 0; i < Width * Height; i++)
    {
        // EPD_2IN66B_Z90_Grey_SendData(0xFF);
        EPD_2IN66B_Z90_Grey_SendData2(0xFF);
    }
    EPD_2IN66B_Z90_Grey_SendCommand(0x26); // write RAM for black(0)/white (1)
    DEV_Digital_Write(EPD_DC_PIN, 1);
    for (i = 0; i < Width * Height; i++)
    {
        // EPD_2IN66B_Z90_Grey_SendData(0x00);
        EPD_2IN66B_Z90_Grey_SendData2(0x00);
    }
    EPD_2IN66B_Z90_Grey_SendCommand(0x22); // Display Update Control
    EPD_2IN66B_Z90_Grey_SendData(0xF7);
    EPD_2IN66B_Z90_Grey_SendCommand(0x20);   // Activate Display Update Sequence
    EPD_2IN66B_Z90_Grey_ReadBusy(30 * 1000); // 9s
}

/******************************************************************************
function :	Sends the image buffer in RAM to e-Paper and displays
parameter:
******************************************************************************/
void EPD_2IN66B_Z90_Grey_Display(unsigned char *BW_Image, unsigned char *R_Image)
{
    UDOUBLE Width, Height;
    Width = (EPD_2IN66B_Z90_Grey_WIDTH % 8 == 0) ? (EPD_2IN66B_Z90_Grey_WIDTH / 8) : (EPD_2IN66B_Z90_Grey_WIDTH / 8 + 1);
    Height = EPD_2IN66B_Z90_Grey_HEIGHT;

    // send black data

    EPD_2IN66B_Z90_Grey_SendCommand(0x24);
    DEV_Digital_Write(EPD_DC_PIN, 1);
    EPD_2IN66B_Z90_Grey_SendData2(BW_Image, Width * Height);

    EPD_2IN66B_Z90_Grey_SendCommand(0x26);
    DEV_Digital_Write(EPD_DC_PIN, 1);
    EPD_2IN66B_Z90_Grey_SendData2(R_Image, Width * Height);

    EPD_2IN66B_Z90_Grey_SendCommand(0x22); // Display Update Control
    EPD_2IN66B_Z90_Grey_SendData(0xC7);
    // EPD_2IN66B_Z90_Grey_SendData(0xF7);
    EPD_2IN66B_Z90_Grey_SendCommand(0x20); // Activate Display Update Sequence
    EPD_2IN66B_Z90_Grey_ReadBusy(30 * 1000);
}

/******************************************************************************
function :	Enter sleep mode
parameter:
******************************************************************************/
void EPD_2IN66B_Z90_Grey_Sleep(void)
{
    EPD_2IN66B_Z90_Grey_SendCommand(0x10); // enter deep sleep
    EPD_2IN66B_Z90_Grey_SendData(0x01);
    DEV_Delay_ms(100);
}

/******************************************************************************
function :	Turn On Display
parameter:
******************************************************************************/
// void EPD_2IN66B_Z90_Grey_TurnOnDisplay(void)
// {
//     EPD_2IN66B_Z90_Grey_SendCommand(0x22); // Display Update Control
//     EPD_2IN66B_Z90_Grey_SendData(0xF7);
//     EPD_2IN66B_Z90_Grey_SendCommand(0x20); // Activate Display Update Sequence
//     EPD_2IN66B_Z90_Grey_ReadBusy(15 * 1000);
// }

void EPD_2IN66B_Z90_Grey_TurnOnDisplay(uint8_t part)
{
    EPD_2IN66B_Z90_Grey_SendCommand(0x22); // Display Update Control
    EPD_2IN66B_Z90_Grey_SendData(part ? 0xC7 : 0xF7);
    EPD_2IN66B_Z90_Grey_SendCommand(0x20); // Activate Display Update Sequence
    EPD_2IN66B_Z90_Grey_ReadBusy(30 * 1000);
}

/******************************************************************************
function :	Sends the image buffer in RAM to e-Paper and displays
parameter:  color:black,0;red,1;*data;index;length
******************************************************************************/
void EPD_2IN66B_Z90_Grey_Data_Write(UBYTE color, UBYTE *Image_Data, UDOUBLE index, UDOUBLE length)
{
    if (index == 0)
    // if (1)
    {
        if (color == 0)
        {
            EPD_2IN66B_Z90_Grey_SendCommand(0x24);
        }
        else if (color == 1)
        {
            EPD_2IN66B_Z90_Grey_SendCommand(0x26);
        }
        DEV_Digital_Write(EPD_DC_PIN, 1);
    }

    UDOUBLE Width, Height;
    Width = (EPD_2IN66B_Z90_Grey_WIDTH % 8 == 0) ? (EPD_2IN66B_Z90_Grey_WIDTH / 8) : (EPD_2IN66B_Z90_Grey_WIDTH / 8 + 1);
    Height = EPD_2IN66B_Z90_Grey_HEIGHT;

    // if(length==(Width*Height))
    // {
    //     for (UDOUBLE j = 0; j < Height; j++)
    //     {
    //         for (UDOUBLE i = 0; i < Width; i++)
    //         {
    //             EPD_2IN66B_Z90_Grey_SendData(Image_Data[i + (Height-j) * Width]);
    //         }
    //     }

    // }
    // else{
#ifdef EPD_SPI_ENABLE
    EPD_2IN66B_Z90_Grey_SendData2(Image_Data, length);
#else
    for (UDOUBLE i = 0; i < length; i++)
    {
        if (color == 0)
        {
            // DEV_Digital_Write(EPD_DC_PIN, 1);
            // DEV_Digital_Write(EPD_CS_PIN, 0);
            EPD_2IN66B_Z90_Grey_SendData2(Image_Data[i]);
            // DEV_Digital_Write(EPD_CS_PIN, 1);
        }
        else if (color == 1)
        {
            // DEV_Digital_Write(EPD_DC_PIN, 1);
            // DEV_Digital_Write(EPD_CS_PIN, 0);
            EPD_2IN66B_Z90_Grey_SendData2(Image_Data[i]);
            // DEV_Digital_Write(EPD_CS_PIN, 1);
        }
    }
#endif
    // }
}

void EPD_2IN66B_Z90_Grey_setDepth(uint8_t i)
{
    iLut = i ? (i > 15 ? 15 : i) : 15;
}

#if 1
int8_t EPD_2IN66B_Z90_Grey_ReadTemp()
{
    int8_t rc=0;

    // EPD_2IN66B_Z90_Grey_SendCommand(0x12); // soft reset
    // EPD_SSD_WaitBusy(100);
    // EPD_2IN66B_Z90_Grey_Reset();

    // EPD_2IN66B_Z90_Grey_ReadBusy(100);
    EPD_2IN66B_Z90_Grey_SendCommand(0x12); // SWRESET
    // EPD_2IN66B_Z90_Grey_ReadBusy(1000);
    DEV_Delay_us(200);

    // Border Waveform
    EPD_2IN66B_Z90_Grey_SendCommand(0x3C);
    EPD_2IN66B_Z90_Grey_SendData(0x05);

    // Temperature sensor control
    EPD_2IN66B_Z90_Grey_SendCommand(0x18);
    EPD_2IN66B_Z90_Grey_SendData(0x80); // 80: internal sensor 48: external sensor

    // Display update control
    EPD_2IN66B_Z90_Grey_SendCommand(0x22);
    EPD_2IN66B_Z90_Grey_SendData(0xb1); // full: 0xf7

    // Master Activation
    EPD_2IN66B_Z90_Grey_SendCommand(0x20);
    // EPD_SSD_WaitBusy(100);
    EPD_2IN66B_Z90_Grey_ReadBusy();

    // read temperature
    EPD_2IN66B_Z90_Grey_SendCommand(0x1b);
    rc = EPD_SSD_ReadData();

    return rc;
}
#endif



